﻿import unittest


from model.gamestate import *
from ui.results_general import ResultsGeneral, creating_result_table,\
    len_of_guessed_words, len_of_failed_words
import random
from presenter_mock import PresenterMock
from kivy import Logger


class ResultsGeneralTest(unittest.TestCase):
    def setUp(self):
        players = Players([Player(u'Andrew', []), Player(u'Petr', []),
                           Player(u'Ivan', []), Player(u'Olga', []),
                           Player(u'Misha', [])])
        players.set_next_pair_strategy(OddStrategy(players.players))
        hat = Hat([u'стул', u'шапка', u'кастрюлька'])
        self.rounds = []
        self.failed = 0
        self.guessed = 0
        self.andrew = 0

        for j in range(0, random.randint(0, 100)):
            words = []
            for i in range(0, random.randint(0, 10)):
                x = random.randint(0, 3)
                words.append(WordGuessResult(
                    Word(players.players[random.randint(0, 2)], 'sometext'),
                    x, 1))
                if x == 1:
                    self.failed += 1
                elif x == 0:
                    self.andrew += 1
                    self.guessed += 1

            self.rounds.append(GameRound(5, words,
                                         Word(players.players[0], 'text'),
                                         [players.players[0],
                                          players.players[1]],
                                         False))

        self.gamestate = GameState(players, hat, None, 0,
                                   self.rounds, None, Settings(20, 30))
        self.RGT = ResultsGeneral()
        self.presenter = PresenterMock(Phase.FROM_TO)
        self.RGT.init_widget(self.gamestate, self.presenter)

    def test_init_widget(self):
        self.assertEqual(self.failed, self.RGT.words_failed)
        self.assertEqual(len(self.gamestate.hat.words),
                         self.RGT.words_remaining)

    def test_table_butt_clicked(self):
        self.RGT.table_butt_clicked(1)
        self.RGT.table_butt_clicked(2)
        self.assertEqual(self.RGT.sortid, 2)
        self.assertEqual(self.RGT.rev, True)
        self.RGT.table_butt_clicked(2)
        self.assertEqual(self.RGT.sortid, 2)
        self.assertEqual(self.RGT.rev, False)

    def test_creating_result_table(self):
        dict_of_pair_results, dict_of_guessed_players_results, \
            dict_of_described_players_results, \
            dict_of_guessed_and_described_players_results = \
            creating_result_table(self.gamestate)

        result_for_person_guessed = {}
        result_for_person_described = {}
        result_for_pair = {}
        result_for_person_guessed_and_described = {}
        result_for_person_guessed_and_described = {}
        players = self.gamestate.players.players
        for player in players:
            result_for_person_guessed[player.name] = 0
            result_for_person_described[player.name] = 0

        for r in self.rounds:
            p1, p2 = (r.players_pair[0].name, r.players_pair[1].name)
            guessed = len_of_guessed_words(r.words)
            result_for_person_guessed[p1] += guessed
            result_for_person_described[p2] += guessed

        self.assertEqual(self.andrew,
                         dict_of_described_players_results[u'Andrew'])

        self.assertEqual(dict_of_described_players_results[u'Andrew'],
                         dict_of_guessed_players_results[u'Petr'])

        for p in self.gamestate.players.players:
            print p.name
            self.assertEqual(result_for_person_described[p.name],
                             dict_of_guessed_players_results[p.name])

            self.assertEqual(result_for_person_guessed[p.name],
                             dict_of_described_players_results[p.name])

    def test_len_of_guessed_words(self):
        guessed = 0
        for r in self.rounds:
            guessed += len_of_guessed_words(r.words)
        self.assertEqual(guessed, self.guessed)

    def test_len_of_failed_words(self):
        failed = 0
        for r in self.rounds:
            failed += len_of_failed_words(r.words)
        self.assertEqual(failed, self.failed)

if __name__ == '__main__':
    unittest.main()
